﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.SystemCore.DTO.Role;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.View.Role;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.SystemCore;

/// <summary>
///     角色业务层
/// </summary>
public class RoleService
{
    #region 获取角色列表

    public async Task<RequestResponseModel> GetRoleList(RoleSearchDTO roleSearchDTO)
    {
        //筛选
        var query = _context.Role
                .ProjectTo<RoleView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(a => a.Name.Contains(roleSearchDTO.Keyword) || a.Remark.Contains(roleSearchDTO.Keyword),
                    !string.IsNullOrEmpty(roleSearchDTO.Keyword))
            ;

        //返回列表
        var role = await query
            .Page(roleSearchDTO.PageIndex, roleSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<RoleView>(role, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增角色

    public async Task<RequestResponseModel> AddRole(RoleAddDTO roleAddDTO)
    {
        //生成新数据
        var role = _mapper.Map<RoleModel>(roleAddDTO);

        role.Id = Guid.NewGuid();
        role.CreateTime = DateTime.Now;

        //新增数据
        _context.Role.Add(role);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(role.Id, "新增成功");
    }

    #endregion

    #region 更新角色

    public async Task<RequestResponseModel> PutRole(RoleUpdateDTO roleUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.Role, roleUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("角色Id无效");
        ;

        //编辑数据
        var role = _mapper.Map(roleUpdateDTO, query);

        //更新表
        _context.Role.Update(role);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(roleUpdateDTO.Id);
    }

    #endregion

    #region 删除角色

    public async Task<RequestResponseModel> DeleteRole(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.Role, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("角色Id无效");
        ;

        //删除操作
        _context.Role.Remove(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion

    #region 批量删除角色按钮

    public async Task<RequestResponseModel> DeleteArrayRole(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.Role
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("字段配置按钮Id无效");
        ;

        //删除操作
        _context.Role.RemoveRange(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "删除成功");
    }

    #endregion

    #region 初始化

    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public RoleService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #endregion
}